window.onload = function () {
    document.getElementById("Announcement-management1").className = "add_class";
    document.getElementById("inform").className = "add-nav-active";
    station_list();
};

// 分页
function pagenum(dataTotals = 0, page = 1) {
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal: Math.ceil(parseInt(dataTotals, 10) / 20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag: true, //是否显示数据统计
        showSkipInputFlag: true, //是否支持跳转
        getPage: function (page) {
            station_list(page);
        }
    });
}


// 请求数据
function station_list(page = 1) {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    // xhr.open('post', "/announcements/administration.html", true);
    // xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    // document.cookie = "page=" + page;
    // xhr.send("page=" + String(page));
    // xhr.onreadystatechange = function () {
    //     if (xhr.readyState === 4) {
    //         if (xhr.status === 200) {
    //             document.getElementById("loading").style.display = "none";
    //             var JsonDate = JSON.parse(xhr.responseText);
    //             create_table(JsonDate["count_num"], JsonDate["data"]);
    //             pagenum(JsonDate["count_num"], page);
    //         }
    //     }
    // };
}


// 创建公告表格
function create_table(count_num, json_data) {
    var t_table = document.getElementById("t_table");
    deltr();
    if (json_data.length === 0) {
        document.getElementById("img-display").style.display = "inline-block";
    } else {
        document.getElementById("img-display").style.display = "none";
    }
    for (var i = 0; i < json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var td1 = document.createElement("td");
        var td2 = document.createElement("td");
        var td3 = document.createElement("td");
        var td4 = document.createElement("td");
        var td5 = document.createElement("td");
        var td6 = document.createElement("td");
        td.innerHTML = "<input id='" + json_data[i]["id"] + "' class='check_box' type=\"checkbox\">";
        td.style.background = "#f8f9fa";
        td.style.textAlign = "center";
        // td.setAttribute("onclick", "check_more_one_not(" + json_data[i]["id"] + ")");
        td1.innerHTML = "<a href='detail.html?id=" + json_data[i]["id"] + "' target=\"_blank\">" + json_data[i]["name"] + "</a>";
        td1.title = json_data[i]["name"];td1.style.paddingLeft = "1%";
        td2.innerHTML = json_data[i]["open"];
        td2.title = json_data[i]["open"];
        td2.style.textAlign = "center";
        td3.innerHTML = json_data[i]["accept_people"];
        td3.title = json_data[i]["accept_people"];
        td4.innerHTML = json_data[i]["announcement_time"];
        td4.title = json_data[i]["announcement_time"];
        td4.style.textAlign = "center";
        td5.innerHTML = json_data[i]["create_people"];
        td5.title = json_data[i]["create_people"];
        td5.style.textAlign = "center";

        td6.innerHTML = "<a href='#' style='color: red;' onclick='evaluation("+json_data[i]["id"]+")'>删除</a>";
        td6.style.textAlign = "center";
        // tr.appendChild(td);
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        tr.appendChild(td4);
        tr.appendChild(td5);
        tr.appendChild(td6);
        t_table.appendChild(tr);
    }
}

function evaluation(ele_id) {
    alert(ele_id)
}

// 获取cookies
function getCookie(objName) {//获取指定名称的cookie的值
    var arrStr = document.cookie.split("; ");
    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        if (temp[0] === objName) {
            return decodeURI(temp[1]);
        }
    }
}

// 删除公告列表
function deltr() {
    var tb = document.getElementById('t_table');
    var rowNum = tb.rows.length;
    for (i = 0; i < rowNum; i++) {
        tb.deleteRow(i);
        rowNum = rowNum - 1;
        i = i - 1;
    }
}


// 选择删除公告
function delete_notice(ele_id) {
    document.getElementById("close-model2").click();
    var page = getCookie("page");
    var myArray = new Array();
    myArray.push(ele_id)
    if (myArray.length === 0) {
        document.getElementById("close-model2").click();
        return
    }
    document.getElementById("loading").style.display = "inline-block";
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/announcements/administration.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("page="+String(page)+"&delete_list=" + String(myArray[0]));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                create_table(JsonDate["count_num"], JsonDate["data"]);
                pagenum(JsonDate["count_num"], page);
                document.getElementById("loading").style.display = "none";
                console.log("ok")
            }
        }
    };
}


// checkBox不允许多选
function check_more_one_not(ele_id) {
    var check_box_list = document.getElementsByClassName("check_box");

    for (var i = 0; i < check_box_list.length; i++) {
        if (String(check_box_list[i].id) !== String(ele_id)) {
            document.getElementById(check_box_list[i].id).checked = false;
        }
    }
}


// 确认删除
function Confirm_delete1() {
    var myArray = new Array();
    var check_box_list = document.getElementsByClassName("check_box");
    for (var t = 0; t < check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            myArray.push(check_box_list[t].id)
        }
    }
    if (myArray.length === 0) {
        document.getElementById("Confirm-content").innerHTML = "<i style='color: #1E90FF;font-size: 50px;vertical-align:middle;' class=\"iconfont icon-jinggao-chucuo\"></i> <span>请选择需要删除的通知！</span>";
    } else {
        document.getElementById("Confirm-content").innerHTML = "<i style='color: #1E90FF;font-size: 50px;vertical-align:middle;' class=\"iconfont icon-jinggao-chucuo\"></i> <span>确认要删除这此通知吗？</span>"
    }
}
